package com.example.loopyserver.common;

import lombok.Data;

import java.io.Serializable;


@Data
public class CommonResult<T> implements Serializable {
    private Integer code;

    private String msg;

    private T data;

    private CommonResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static CommonResult<Void> build(ResultCode code) {
        return new CommonResult<>(code.value, code.defaultMsg, null);
    }

    public static CommonResult<Void> build(ResultCode code, String msg) {
        return new CommonResult<>(code.value, msg, null);
    }

    public static <T> CommonResult<T> build(ResultCode code, T data) {
        return new CommonResult<>(code.value, code.defaultMsg, data);
    }

    public static <T> CommonResult<T> build(ResultCode code, String msg, T data) {
        return new CommonResult<>(code.value, msg, data);
    }

    public static <T> CommonResult<T> success(T data) {
        return build(ResultCode.SUCCESS, data);
    }

    public static CommonResult<Void> error(ResultCode code) {
        return build(code);
    }

    public static CommonResult<Void> error(ResultCode code, String msg) {
        return build(code, msg);
    }
}
